#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: PlayersRepository.cs
//

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Ids.Web.Listing;
using Novagoal.Models;
using Novagoal.Models.Utils;
using Novagoal.Repositories.Extensions;

namespace Novagoal.Repositories.Entities
{
    public class PlayersRepository : BaseRepository, IPlayersRepository
    {
        #region CONSTANTS



        #endregion

        #region PROPERTIES

        

        #endregion

        #region PUBLIC METHODS

        public List<Player> GetPlayers(Dictionary<PlayerListFilter, string> filterList, PlayerListSortField sortField, SortDirectionType sortDirection, int currentPage, int pageSize)
        {
            var qryPlayers = FilterPlayers(filterList);

            var players = SortTeams(qryPlayers, sortField, sortDirection)
                .TakePage(currentPage, pageSize)
                .AsEnumerable()
                .Select(CreatePlayer)
                .ToList();

            return players;
        }

        public int GetTotalPlayers(Dictionary<PlayerListFilter, string> filterList)
        {
            return FilterPlayers(filterList).Count();
        }

        public void UpdateMvp(int playerId)
        {
            var playerEntity = EntitiesContext.players
                .Where(p => p.id == playerId)
                .FirstOrDefault();

            playerEntity.mvp++;
            EntitiesContext.SaveChanges();
        }

        public Player GetPlayer(int playerId)
        {
            return EntitiesContext.players
                .Where(p => p.id == playerId)
                .Select(CreatePlayer)
                .FirstOrDefault();
        }

        public int AddPlayer(Player player)
        {
            var entity = new players
                             {
                                 name = player.Name,
                                 position = player.Position,
                                 mvp = player.Mvp,
                                 created_at = UniversalDate,
                                 updated_at = UniversalDate,
                                 updated_by = player.UpdatedBy
                             };

            EntitiesContext.AddToplayers(entity);
            EntitiesContext.SaveChanges();

            return entity.id;
        }

        public int EditPlayer(Player player)
        {
            var entity = EntitiesContext.players
                .Where(p => p.id == player.Id)
                .FirstOrDefault();

            entity.name = player.Name;
            entity.position = player.Position;
            entity.mvp = player.Mvp;
            entity.updated_at = UniversalDate;
            entity.updated_by = player.UpdatedBy;

            EntitiesContext.SaveChanges();

            return entity.id;
        }

        #endregion

        #region PRIVATE METHODS

        private IQueryable<players> FilterPlayers(Dictionary<PlayerListFilter, string> filters)
        {
            var players = EntitiesContext.players as IQueryable<players>;

            if (filters != null)
            {
                if (filters.Keys.Contains(PlayerListFilter.Id))
                {
                    int id;
                    if (int.TryParse(filters[PlayerListFilter.Id], out id))
                    {
                        return players.Where(item => item.id == id);
                    }
                }
                if (filters.Keys.Contains(PlayerListFilter.Mvp))
                {
                    int mvp;
                    if (int.TryParse(filters[PlayerListFilter.Mvp], out mvp))
                    {
                        return players.Where(item => item.mvp == mvp);
                    }
                }
                if (filters.Keys.Contains(PlayerListFilter.Name))
                {
                    var strName = filters[PlayerListFilter.Name];
                    players = players.Where(item => item.name.Contains(strName));
                }
                if (filters.Keys.Contains(PlayerListFilter.Position))
                {
                    var strPosition = filters[PlayerListFilter.Position];
                    players = players.Where(item => item.position.Contains(strPosition));
                }
            }

            return players;
        }

        private IQueryable<players> SortTeams(IQueryable<players> query, PlayerListSortField sortField, SortDirectionType sortDirection)
        {
            switch (sortField)
            {
                case PlayerListSortField.Id:
                    return query.OrderBy(item => item.id, sortDirection);

                case PlayerListSortField.Name:
                    return query.OrderBy(item => item.name, sortDirection);

                case PlayerListSortField.Position:
                    return query.OrderBy(item => item.position, sortDirection);

                case PlayerListSortField.Mvp:
                    return query.OrderBy(item => item.mvp, sortDirection);

                case PlayerListSortField.CreatedAt:
                    return query.OrderBy(item => item.created_at, sortDirection);

                case PlayerListSortField.UpdatedAt:
                    return query.OrderBy(item => item.updated_at, sortDirection);

            }
            return query.OrderBy(item => item.name);
        }

        #endregion
    }
}